package com.dragons.spring5.transaction;

import com.dragons.spring5.User;
import com.dragons.spring5.transaction.config.TxConfig;
import com.dragons.spring5.transaction.service.AccountService;
import com.dragons.spring5.transaction.service.AccountService1;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext;

public class TestTransaction {
    @Test
    public void test(){
        BeanFactory context = new ClassPathXmlApplicationContext("transaction.xml");
        AccountService accountService = context.getBean("accountService",AccountService.class);
        accountService.accountMoney();
    }

    @Test //完全注解事务
    public void test1(){
        ApplicationContext context = new AnnotationConfigApplicationContext(TxConfig.class);
        AccountService1 accountService1 = context.getBean("accountService1",AccountService1.class);
        accountService1.accountMoney();
    }


    //函数式风格创建对象，交给 spring 进行管理
    @Test
    public void testGenericApplicationContext() {
        //1 创建 GenericApplicationContext 对象
        GenericApplicationContext context = new GenericApplicationContext();
        //2 调用 context 的方法对象注册
        context.refresh();
        context.registerBean("user111", User.class,() -> new User());
        //3 获取在 spring 注册的对象
        // User user = (User)context.getBean("com.atguigu.spring5.test.User");
        User user = (User)context.getBean("user111");
        System.out.println(user);
    }
}
